package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.*;
import cn.tedu.carehome.foundation.pojo.dto.BedAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.BedUpdateDTO;
import cn.tedu.carehome.foundation.pojo.dto.OlderStaffAddDTO;
import cn.tedu.carehome.foundation.pojo.dto.OlderStaffUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.Bed;
import cn.tedu.carehome.foundation.pojo.entity.Room;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.service.IBedService;
import cn.tedu.carehome.foundation.service.IOlderStaffService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public  class OlderStaffLinkServiceImpl implements IOlderStaffService {
    @Autowired
    OtherMapper otherMapper;

    @Autowired
    OlderMapper olderMapper;

    @Autowired
    StaffMapper staffMapper;




    @Override
    public List<OlderStaffListItemVO> list() {
        List<OlderStaffListItemVO> list = otherMapper.list();
        return list;
    }

    @Override
    public void insert(OlderStaffAddDTO olderStaffAddDTO) {
        //检查员工和老人是否存在

        OlderStandardVO olderStandardVO= olderMapper.getStandardById(olderStaffAddDTO.getOlderId());
        if (olderStandardVO==null){
            String message= "新增失败!老人不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        StaffStandardVO staffStandardVO= staffMapper.getStandardById(olderStaffAddDTO.getStaffId());
        if (staffStandardVO==null){
            String message= "新增失败!员工不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //检查老人是否已经关联员工

        StaffStandardVO staffStandardVO1 =  otherMapper.getStaffByOlderIdReal(olderStaffAddDTO.getOlderId());
        if (staffStandardVO1!=null){
            String message= "新增失败!该老人已经绑定员工:["+staffStandardVO1.getUsername()+"]!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //执行关联
        int rows = otherMapper.insert(olderStaffAddDTO);
        if (rows!=1){
            String message = "新增失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }





    }

    @Override
    public void deleteByLinkId(Long id) {
        //查询是否存在
        OlderStaffStandardVO olderStaffStandardVO =  otherMapper.selectStandardById(id);
        if (olderStaffStandardVO==null){
            String message = "删除失败，关联不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //执行删除
        int rows  = otherMapper.deleteLinkById(id);
        if (rows!=1){
            String message = "失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

    @Override
    public OlderStaffStandardVO selectStandardById(Long id) {
        //是否存在
        OlderStaffStandardVO olderStaffStandardVO=otherMapper.selectStandardById(id);
        if (olderStaffStandardVO==null){
            String message = "失败，不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return olderStaffStandardVO;
    }

    @Override
    public void updateLink(OlderStaffUpdateDTO olderStaffUpdateDTO) {
        //查询老人和员工是否存在

        OlderStandardVO olderStandardVO= olderMapper.getStandardById(olderStaffUpdateDTO.getOlderId());
        if (olderStandardVO==null){
            String message= "修改失败!老人不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        StaffStandardVO staffStandardVO= staffMapper.getStandardById(olderStaffUpdateDTO.getStaffId());
        if (staffStandardVO==null){
            String message= "修改失败!员工不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //判断老人是否绑定了员工,没有绑定不允许修改

        StaffStandardVO staffStandardVO1 =  otherMapper.getStaffByOlderIdReal(olderStaffUpdateDTO.getOlderId());
        if (staffStandardVO1 == null){
            String message= "修改失败!该老人还没有关联员工";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //执行修改
        int rows = otherMapper.updateLink(olderStaffUpdateDTO);
        if (rows!=1){
            String message = "失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

}
